package com.yixun.qfbao.hander;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.common.page.Page;
import com.yixun.qfbao.api.service.DataCenterService;
import com.yixun.qfbao.apidto.GetPremisesFlowExportReqDto;
import com.yixun.qfbao.apidto.GetPremisesFlowExportResDto;
import com.yixun.qfbao.apidto.GetPremisesFlowExportResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.FlowRecordDto;
import com.yixun.qfbao.model.enums.PartnerInfoEnum;
import com.yixun.qfbao.service.FlowRecordService;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetPremisesFlowExportReqVo;
import com.yixun.qfbao.xf.model.CaseMember;
import com.yixun.qfbao.xf.model.dto.CaseMemberDto;
import com.yixun.qfbao.xf.model.enums.PremisesStatusEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.CaseMemberService;

/**
 * @author guifang.weng
 * @create 2017-11-28 11:20
 **/
@Service
public class GetPremisesFlowExportHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetPremisesFlowExportHander.class);

    @Autowired
    private FlowRecordService flowRecordService;
    @Autowired
    private LoginUtils loginUtils;
    @Autowired
    private CaseMemberService caseMemberService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<GetPremisesFlowExportResDto> res = new BaseResponseDTO<>();
        GetPremisesFlowExportReqVo reqVo = (GetPremisesFlowExportReqVo)reqDto;
        GetPremisesFlowExportReqDto getPremisesFlowExportReqDto = reqVo.getParams();
        GetPremisesFlowExportResDto resDto = new GetPremisesFlowExportResDto();
        try {
        	FlowRecordDto frDto = new FlowRecordDto();
        	frDto.setObjectId(getPremisesFlowExportReqDto.getObjectId());
        	//拼接时间
        	String beginDate = getPremisesFlowExportReqDto.getBeginDate();
        	String endDate = getPremisesFlowExportReqDto.getEndDate();
        	        	
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	if(StringUtils.isNotBlank(beginDate)) {
        		beginDate = beginDate + " 00:00:00";
        		frDto.setCreateTimeBefore(sdf.parse(beginDate));
        	}
        	if(StringUtils.isNotBlank(endDate)) {
        		endDate = endDate + " 23:59:59";
        		frDto.setCreateTimeAfter(sdf.parse(endDate));
        	}
        	List<FlowRecordDto> data = new ArrayList<FlowRecordDto>();
        	PartnerInfo pi = loginUtils.getUser(getPremisesFlowExportReqDto.getUserId());
            if (null == pi) {
            	resDto.setData(data);
            	resDto.setResult(ResultEnum.SUCCESS.getNo());
                return res;
            }
            if(PartnerInfoEnum.ACCOUNT_MECHANISM.getCode().equals(pi.getAccountType())) {
            	//管理员: 查询公司下所有信息
            	frDto.setHigherId(pi.getHigherid());
            }else {
            	//经纪人: 查询驻守楼盘信息
            	List<Long> premisesIds = new ArrayList<Long>();
            	CaseMemberDto entity = new CaseMemberDto();
                entity.setPremisesStatus(PremisesStatusEnum.type1.getNo());
                entity.setCompanyId(pi.getHigherid());
                entity.setPartnerId(pi.getId());
                List<CaseMember> list = caseMemberService.selectListByDto(entity);
                for (CaseMember cm : list) {
                	premisesIds.add(cm.getPremisesId());
                }
                frDto.setPremisesIds(premisesIds);
                if(null == premisesIds || premisesIds.size() == 0) {
                	resDto.setData(data);
                	resDto.setResult(ResultEnum.SUCCESS.getNo());
                    return res;
                }
            }
            frDto.setOrders("b.click_count desc");
            data = flowRecordService.premisesFlowExportData(frDto);
        	resDto.setData(data);
        	resDto.setResult(ResultEnum.SUCCESS.getNo());
        	res.setResData(resDto);
        }catch (Exception e) {
            logger.error("GetPremisesFlowExportHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("流量分析表导出失败");
        }
        return res;
    }
}
